/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "core/modelmanager.h"
#include "base/launcheritem.h"
//#include "KylinItemView/kquickgridview.h"
#include "logger.h"

int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;

#if 0
    ModelManager modelMgr;
    MultiPageModel launcherModel;

    const QString names[9] = {
        "red", "green", "blue",
        "yellow", "gray", "pink",
        "darkBlue", "lightGray", "purple"
    };
    const int types[9] = { 0, 0, 1, 0, 1, 1, 1, 0, 1 };
    for (int page=0; page<3; ++page) {
        PageModel *pModel = new PageModel;
        for (int index=0; index<9; ++index) {
            LauncherItem *item = new LauncherItem;
            item->setName(names[index % 9]);
            item->setType(types[index % 9]);
            item->setPageIndex(page);
            pModel->append(item);
        }
        launcherModel.append(pModel);
    }
    modelMgr.setPageModel(launcherModel);
    engine.rootContext()->setContextProperty("modelManager", &modelMgr);

    qmlRegisterType<LauncherItem>("org.ukui.tablet", 1, 0, "LauncerItem");

    qmlRegisterType<ModelManager>("com.test", 1, 0, "ModelManager");
    qmlRegisterType<KQuickGridView>("com.test", 1, 0, "KGridView");
    qmlRegisterType<LauncherItem>("com.test", 1, 0, "LauncherItem");
    common::QmlLogger logger;
    qInstallMessageHandler(common::Logger::outputMessage);
    engine.rootContext()->setContextProperty("qmlLogger", &logger);
#endif

    ModelManager modelMgr;
    engine.rootContext()->setContextProperty("modelManager", &modelMgr);
    qmlRegisterType<LauncherItem>("org.ukui.tablet", 1, 0, "LauncherItem");
    qmlRegisterType<CategoryItem>("org.ukui.tablet", 1, 0, "CategoryItem");
    const QUrl url(QStringLiteral("qrc:/andApp.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}
